package stringfmt
import(
    "strings"
    "bytes"
    "regexp"
    "math/big"
    "crypto/rand"
    "strconv"
    "github.com/astaxie/beego"
    "net/smtp"
    "time"

)

// StrArrayFristToUpper 字符串首字母转化为大写
func StrArrayFristToUpper(arry []string) []string {
    for k, _ := range arry {
        arry[k] = StrFirstToUpper(arry[k])
    }
    return arry
}

// StrFirstToUpper  字符串首字母转化为大写 ios_bbbbbbbb -> iosBbbbbbbbb
func StrFirstToUpper(str string) string {
    b1 := new(bytes.Buffer)
    for i := 0; i < len(str); i++ {
        if i == 0 {
            tmp := string(str[i])
            b1.WriteString(strings.ToUpper(tmp))
        } else {
            b1.WriteByte(str[i])
        }
    }
    return b1.String()
}






//Substr 截取字符串 start 起点下标 length 需要截取的长度
func Substr(str string, start int, length int) string {
    rs := []rune(str)
    rl := len(rs)
    end := 0

    if start < 0 {
        start = rl - 1 + start
    }
    end = start + length

    if start > end {
        start, end = end, start
    }

    if start < 0 {
        start = 0
    }
    if start > rl {
        start = rl
    }
    if end < 0 {
        end = 0
    }
    if end > rl {
        end = rl
    }

    return string(rs[start:end])
}

//Substr2 截取字符串 start 起点下标 end 终点下标(不包括)
func Substr2(str string, start int, end int) string {
    rs := []rune(str)
    length := len(rs)

    if start < 0 || start > length {
        panic("start is wrong")
    }

    if end < 0 || end > length {
        panic("end is wrong")
    }

    return string(rs[start:end])
}

//GetBetweenStr 获取字符串 str在substr之后的
func GetBetweenStr(str, substr string) string {
    n := strings.Index(str, substr)
    if n == -1 {
        n = 0
    }
    str = string([]byte(str)[n:])
    return str
}

// GetFileSuffix 获取文件扩展名
func GetFileSuffix(s string) string {
    re, _ := regexp.Compile(".(jpg|jpeg|png|gif|exe|doc|docx|ppt|pptx|xls|xlsx)")
    suffix := re.ReplaceAllString(s, "")
    return suffix
}

func RandInt64(min, max int64) int64 {
    maxBigInt := big.NewInt(max)
    i, _ := rand.Int(rand.Reader, maxBigInt)
    if i.Int64() < min {
        RandInt64(min, max)
    }
    return i.Int64()
}

func Strim(str string) string {
    str = strings.Replace(str, "\t", "", -1)
    str = strings.Replace(str, " ", "", -1)
    str = strings.Replace(str, "\n", "", -1)
    str = strings.Replace(str, "\r", "", -1)
    return str
}

func Unicode(rs string) string {
    json := ""
    for _, r := range rs {
        rint := int(r)
        if rint < 128 {
            json += string(r)
        } else {
            json += "\\u" + strconv.FormatInt(int64(rint), 16)
        }
    }
    return json
}

func HTMLEncode(rs string) string {
    html := ""
    for _, r := range rs {
        html += "&#" + strconv.Itoa(int(r)) + ";"
    }
    return html
}

/**
 *  to: example@example.com;example1@163.com;example2@sina.com.cn;...
 *  subject:The subject of mail
 *  body: The content of mail
 */
func SendMail(to string, subject string, body string) error {
    user := beego.AppConfig.String("mailfrom")
    password := beego.AppConfig.String("mailpassword")
    host := beego.AppConfig.String("mailhost")

    hp := strings.Split(host, ":")
    auth := smtp.PlainAuth("", user, password, hp[0])
    var content_type string
    content_type = "Content-type:text/html;charset=utf-8"

    msg := []byte("To: " + to + "\r\nFrom: " + user + "<" + user + ">\r\nSubject: " + subject + "\r\n" + content_type + "\r\n\r\n" + body)
    send_to := strings.Split(to, ";")
    err := smtp.SendMail(host, auth, user, send_to, msg)
    return err
}

//数组去重来源网络
func RemoveDuplicatesAndEmpty(a []string) (ret []string) {
    a_len := len(a)
    for i := 0; i < a_len; i++ {
        if (i > 1 && a[i-1] == a[i]) || len(a[i]) == 0 {
            continue
        }
        ret = append(ret, a[i])
    }
    return
}

/*
 * 字符产转换为time.Time
 */
func EncodeTime(toBeCharge string) time.Time {
    timeLayout := "2006-01-02 15:04:05"
    loc, _ := time.LoadLocation("Local")
    theTime, _ := time.ParseInLocation(timeLayout, toBeCharge, loc)
    return theTime
}

//Desc 任意类型的数组转换为字符串
//Param sl string, params []interface{}
//return str string
func SliceToStr(sl string, params []int64) (str string) {
    var paramSlice []string
    beego.Debug(params)
    for _, param := range params {
        beego.Debug(param)
        paramSlice = append(paramSlice, strconv.FormatInt(param, 10))
    }
    str = strings.Join(paramSlice, sl) // Join 方法第2个参数是 string 而不是 rune
    return
}

//SliToStr 任意类型的数组转换为字符串
//Param  sl string,params []interface{}
//return str string
func SliToStr(sl string, params ...interface{}) (str string) {
    var paramSlice []string
    for _, param := range params {
        paramSlice = append(paramSlice, param.(string))
    }
    str = strings.Join(paramSlice, sl) // Join 方法第2个参数是 string 而不是 rune
    return
}
